<template>
  <header>
    <h2>
      产品月销
    </h2>
    <div class="chart" id="twoChart">
        图表内容
    </div>
  </header>
</template>

<script lang="ts">
import { inject, onMounted, reactive } from 'vue'
export default {
  setup () {
    const $echarts:any = inject('echarts')

    // 创建一个对象变量
    const chartData:any = {
      day: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'],
      Clothes: [180, 280, 160, 235, 160, 270, 194],
      digit: [322, 412, 540, 672, 462, 564, 353],
      Electrical: [500, 764, 650, 864, 960, 750, 888],
      gear: [650, 980, 1020, 1470, 1360, 1460, 1005],
      Chemicals: [864, 2010, 2406, 2345, 2680, 2840, 2140]
    }

    onMounted(() => {
      // init初始化echarts图表
      const myChart = $echarts.init(document.getElementById('twoChart'))
      myChart.setOption({
        // 悬浮提示框
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#e6b600'
            }
          }
        },
        legend: {
          data: ['服饰', '数码', '家电', '家居', '日化']
        },
        // 设置图表距离
        grid: {
          left: '1%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          // 使起点与Y轴无间隙
          boundaryGap: false,
          data: chartData.day,
          axisLine: {
            lineStyle: {
              color: '#fff'
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            lineStyle: {
              color: '#fff'
            }
          }
        },
        series: [
          {
            name: '服饰',
            type: 'line',
            data: chartData.Clothes,
            // 使折线平滑
            smooth: true,
            // 隐藏折点
            showSymbol: false,
            // 使数据堆叠
            stack: 'Total',
            // 隐藏区域最上方的线段
            lineStyle: {
              width: 0
            },
            // 选中区域高亮
            emphasis: {
              focus: 'series'
            },
            // 填充区域样式
            areaStyle: {
              opacity: 0.8,
              // 配置渐变颜色
              color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(128, 255, 165)'
                },
                {
                  offset: 1,
                  color: 'rgb(1, 191, 236)'
                }
              ])
            }
          },
          {
            name: '数码',
            type: 'line',
            data: chartData.digit,
            smooth: true,
            showSymbol: false,
            stack: 'Total',
            lineStyle: {
              width: 0
            },
            emphasis: {
              focus: 'series'
            },
            areaStyle: {
              opacity: 0.8,
              color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(0, 221, 255)'
                },
                {
                  offset: 1,
                  color: 'rgb(77, 119, 255)'
                }
              ])
            }
          },
          {
            name: '家电',
            type: 'line',
            data: chartData.Electrical,
            smooth: true,
            showSymbol: false,
            stack: 'Total',
            lineStyle: {
              width: 0
            },
            emphasis: {
              focus: 'series'
            },
            areaStyle: {
              opacity: 0.8,
              color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(55, 162, 255)'
                },
                {
                  offset: 1,
                  color: 'rgb(116, 21, 219)'
                }
              ])
            }
          },
          {
            name: '家居',
            type: 'line',
            data: chartData.gear,
            smooth: true,
            showSymbol: false,
            stack: 'Total',
            lineStyle: {
              width: 0
            },
            emphasis: {
              focus: 'series'
            },
            areaStyle: {
              opacity: 0.8,
              color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(255, 0, 135)'
                },
                {
                  offset: 1,
                  color: 'rgb(135, 0, 157)'
                }
              ])
            }
          },
          {
            name: '日化',
            type: 'line',
            data: chartData.Chemicals,
            smooth: true,
            showSymbol: false,
            stack: 'Total',
            lineStyle: {
              width: 0
            },
            emphasis: {
              focus: 'series'
            },
            areaStyle: {
              opacity: 0.8,
              color: new $echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(255, 191, 0)'
                },
                {
                  offset: 1,
                  color: 'rgb(224, 62, 76)'
                }
              ])
            }
          }
        ]
      })
    })
  }
}
</script>
<style lang='scss' scoped>
.chart {
  height: 4.375rem;
}
h2 {
  height: .3125rem;
  color: #fff;
  line-height: .3125rem;
  font-size: .25rem;
  text-align: center;
}
</style>
